/**
 * @author Jeremy Bergan
 *
 */

package autoincrementor;


import java.awt.event.ActionEvent;
import javax.swing.AbstractAction;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

import com.sun.xml.internal.ws.api.server.Container;



public class AutoIncrement extends JPanel {
	
	JTextPane textPane;
	JTextField fromInc;
	JTextField toInc;
	JTextField incBy;
	JTextField sepBy;
	
	JLabel iF; 
	JLabel iT;
	JLabel iB; 
	JLabel sB;
	JLabel notify;
	
	JButton submit;
	
	public AutoIncrement(AutoIncrementor autoIncrementor, JTextPane pane){
		
		textPane = pane;
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);
		
		fromInc = new JTextField(4);
		toInc = new JTextField(4);
		incBy = new JTextField(4);
		sepBy = new JTextField(4);
		
		
		iF = new JLabel("Increment From:");
		iT = new JLabel("Increment To:");
		iB = new JLabel("Increment By:");
		sB = new JLabel("Separate By:");
		notify = new JLabel("");
		notify.setVisible(false);
		
		submit = new JButton(new SubmitAction(this, autoIncrementor));
		
		layout.setHorizontalGroup(
				layout.createSequentialGroup()
						.addGroup(layout.createParallelGroup()
								.addComponent(iF)
								.addComponent(iT)
								.addComponent(iB)
								.addComponent(sB)
								.addComponent(submit))
						.addGroup(layout.createParallelGroup()
								.addComponent(fromInc)
								.addComponent(toInc)
								.addComponent(incBy)
								.addComponent(sepBy)
								.addComponent(notify))
				
		);
		layout.setVerticalGroup(
				layout.createSequentialGroup()
					.addGroup(layout.createParallelGroup()
						.addComponent(iF)
						.addComponent(fromInc))
					.addGroup(layout.createParallelGroup()
						.addComponent(iT)
						.addComponent(toInc))
					.addGroup(layout.createParallelGroup()
									.addComponent(iB)
									.addComponent(incBy))
					.addGroup(layout.createParallelGroup()
									.addComponent(sB)
									.addComponent(sepBy))
					.addGroup(layout.createParallelGroup()
									.addComponent(submit)
									.addComponent(notify))
								
		
		);	
		
	}
	
	private class SubmitAction extends AbstractAction{
		
		AutoIncrement inc;
		AutoIncrementor autoinc;
		
		public SubmitAction(AutoIncrement arg0, AutoIncrementor arg1){
			super("Submit");
			inc = arg0;
			autoinc = arg1;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			JTextPane txt = inc.textPane;
			Document doc = txt.getDocument();	 
			
			int i = inc.textPane.getCaretPosition();
			notify.setVisible(false);
			
			try {
				int from = Integer.parseInt(inc.fromInc.getText());
				int to = Integer.parseInt(inc.toInc.getText());
				int by = Integer.parseInt(inc.incBy.getText());
			
					
				if((from > to) && (by > 0)){
					//System.out.print("Error:\n");
					notify.setText("This cannot iterate");
					notify.setVisible(true);
					return;
				}
				else if((from < to) && (by < 0)){
					//System.out.print("Error:\n");
					notify.setText("This cannot iterate");
					notify.setVisible(true);
					return;
				}
				if(from > to){
					while(from >= to){
						if(((from+by) < to) ){
							//doc.insertString(i,String.valueOf(from), null);
							txt.setText(txt.getText().substring(0, i)+String.valueOf(from)+txt.getText().substring(i));
						}
						else{
						//doc.insertString(i,String.valueOf(from)+inc.sepBy.getText(), null);
							txt.setText(txt.getText().substring(0, i)+String.valueOf(from)+inc.sepBy.getText()+txt.getText().substring(i));
					}
					
						i = i + (String.valueOf(from)+inc.sepBy.getText()).length();
						from = from + by;
					}
				}
				else{
					while(from <= to){
						if(((from+by) > to) ){
							//doc.insertString(i,String.valueOf(from), null);
							txt.setText(txt.getText().substring(0, i)+String.valueOf(from)+txt.getText().substring(i));
						}
						else{
						//doc.insertString(i,String.valueOf(from)+inc.sepBy.getText(), null);
							txt.setText(txt.getText().substring(0, i)+String.valueOf(from)+inc.sepBy.getText()+txt.getText().substring(i));
					}
					
						i = i + (String.valueOf(from)+inc.sepBy.getText()).length();
						from = from + by;
					}
				}
			} catch (NumberFormatException e2) {
				// TODO Auto-generated catch block
				//System.out.print("Error in Number:\n");
				notify.setText("Not Numbers");
				notify.setVisible(true);
				return;
				
			}
			autoinc.setVisible(false);
			return;
			
		}
		
	}


	
}


